package br.com.rapidrest.chain;

import javax.servlet.http.HttpServletResponse;

import br.com.rapidrest.exception.ChainException;
import br.com.rapidrest.response.SimpleResponseGenerator;

public class SimpleResponseGeneratorCommand implements Command {

	@Override
	public void execute(Chain chain) throws ChainException {
		BasicChainContext context = (BasicChainContext) chain.getContext();

		Object returnedValue = context.getReturnedValue();

		if (returnedValue instanceof SimpleResponseGenerator) {
			SimpleResponseGenerator responseGenerator = (SimpleResponseGenerator) returnedValue;
			HttpServletResponse response = context.getResponse();
			responseGenerator.generate(response);
		} else {
			chain.proceed();
		}
	}

}
